package com.atguigu.gmall.order.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.order.dto.OrderSubmitDto;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.order.entity.WareStockMsg;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/api/order")
public class OrderController {

    @Autowired
    private OrderInfoService orderInfoService ;

    /**
     * @Valid注解使用到实体类型参数的前面，表示该实体类中的相关属性需要开启校验功能
     * @param tradeNo
     * @param orderSubmitDto
     * @return
     */
    @PostMapping(value = "/auth/submitOrder")
    public Result<String>  submitOrder(@RequestParam(value = "tradeNo") String tradeNo , @Valid @RequestBody OrderSubmitDto orderSubmitDto) {
        String orderId = orderInfoService.submitOrder(tradeNo , orderSubmitDto) ;
        return Result.build(orderId , ResultCodeEnum.SUCCESS) ;
    }

    @PostMapping(value = "/orderSplit")
    public List<WareStockMsg> orderSplit(@RequestParam(value = "orderId") Long orderId , @RequestParam(value = "wareSkuMap") String wareSkuMap) {
        return orderInfoService.orderSplit(orderId , wareSkuMap) ;
    }

    @GetMapping(value = "/auth/{pageNo}/{pageSize}")
    public Result<Page<OrderInfo>> findOrderByPage(@PathVariable(value = "pageNo") Integer pageNo , @PathVariable(value = "pageSize") Integer pageSize) {
        Page<OrderInfo> orderInfoPage = orderInfoService.findOrderByPage(pageNo , pageSize) ;
        return Result.build(orderInfoPage , ResultCodeEnum.SUCCESS) ;
    }

}
